package st.business.tepth.business.user.busnumber.finishholder.maintain;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.BusNumberBean;
import st.business.tepth.business.bean.PersonBean;
import st.business.tepth.business.businfo.BusInfoDelegate;
import st.business.tepth.business.event.ReloadBusNumberDataEvent;
import st.business.tepth.business.event.StartBrotherEvent;
import st.business.tepth.business.user.busnumber.workholder.maintain.finish.DialogFinishNotPass;
import st.business.tepth.business.user.busnumber.workholder.maintain.finish.MaintainContentAdapter;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.dialog.IDialogClickListener;
import st.shadow.library.ui.recycler.BaseDecoration;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-11 15:37
 * 保养已完成\待作业
 */

public class FinishMaintainDelegate extends MvpFragment<FinishMaintainPresenter> implements FinishMaintainContract.View {
    public static FinishMaintainDelegate newInstance(BusNumberBean busNumberBean,
                                                     boolean isFinish) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("busNumberBean", busNumberBean);
        bundle.putBoolean("isFinish", isFinish);
        FinishMaintainDelegate delegate = new FinishMaintainDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.recycler_view_tab)
    RecyclerView mRecyclerViewClassification;
    @BindView(R2.id.tv_work_type)
    TextView mTvType;
    @BindView(R2.id.view_pager)
    ViewPager mViewPager;
    @BindView(R2.id.tv_cancel_work)
    TextView mTvReWork;
    private ClassificationAdapter mClassificationAdapter;
    private DialogLoader mDialogLoader;
    //是否已完成
    private boolean isFinish;
    private BusNumberBean mBusNumberBean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBusNumberBean = bundle.getParcelable("busNumberBean");
            isFinish = bundle.getBoolean("isFinish");
        }
    }

    @Override
    public FinishMaintainPresenter getPresenter() {
        return new FinishMaintainPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_maintain_finish;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvType.setText(mBusNumberBean.getStatusName());
        mRecyclerViewClassification.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerViewClassification.addItemDecoration(
                BaseDecoration.create(_mActivity.getResources()
                        .getColor(R.color.fontColorWhite), 2));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerViewClassification.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mRecyclerViewClassification.setAdapter(mClassificationAdapter =
                new ClassificationAdapter(R.layout.item_maintain_finish_classification,
                        null, new OnItemChooseListener<Integer>() {
                    @Override
                    public void onChoose(Integer position) {
                        chooseMaintain(position);
                    }
                }));
        mDialogLoader = new DialogLoader(_mActivity);
        if (mBusNumberBean != null) {
            int status = mBusNumberBean.getStatus();
            List<PersonBean> completeUsers = mBusNumberBean.getCompleteUsers();
            boolean isCompleteUser = false;
            if (completeUsers != null) {
                String myCode = UserManager.getInstance().getCurrentUserCode();
                for (PersonBean personBean : completeUsers) {
                    if (myCode.equals(personBean.getJob_no())) {
                        isCompleteUser = true;
                        break;
                    }
                }
            }
            mTvReWork.setVisibility((isCompleteUser && (status == 8 ||
                    status == 9)) ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.getData(UserManager.getInstance().getToken(), mBusNumberBean.getId(),
                isFinish);
    }

    @Override
    public void showData(List<MultipleItemEntity> list) {
        mClassificationAdapter.setNewData(list);
        if (!list.isEmpty()) {
            chooseMaintain(0);
            mViewPager.setAdapter(new MaintainContentAdapter(getChildFragmentManager(),
                    list));
        }
    }

    @OnClick(R2.id.tv_bus_info)
    public void onShowBusInfo() {
        EventBus.getDefault().post(
                new StartBrotherEvent(BusInfoDelegate.newInstance(mBusNumberBean.getId())));
    }

    private void chooseMaintain(int position) {
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @Override
    public void reworkSucceed() {
        mDialogLoader.stopProgressWithSuccess("返工成功", "请确认", new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                EventBus.getDefault().post(new ReloadBusNumberDataEvent());
            }
        });
    }

    @OnClick(R2.id.tv_cancel_work)
    public void onRework() {
//        new DialogRework(_mActivity, new OnItemChooseListener<String>() {
//            @Override
//            public void onChoose(String s) {
//                mDialogLoader.showProgressDialog(true, "正在提交",
//                        "请稍候...");
//                mPresenter.rework(UserManager.getInstance().getToken(),
//                        mBusNumberBean.getId(), s);
//            }
//        }).show();
        new DialogFinishNotPass(_mActivity, "竣检返工", mClassificationAdapter.getData(),
                new DialogFinishNotPass.OnConfirmListener() {
                    @Override
                    public void onConfirm(String ids, String reason) {
                        mDialogLoader.showProgressDialog(true, "正在提交", "请稍候...");
                        mPresenter.rework(UserManager.getInstance().getToken(),
                                mBusNumberBean.getId(), ids, reason
                        );
                    }
                }).measureSize().show();
    }
}